<template>
    <view class="test-container">
        <view class="header">
            <text class="title">API测试页面</text>
        </view>

        <view class="test-section">
            <view class="section-title">用户相关API (8080端口)</view>

            <view class="test-item">
                <button class="test-btn" @click="testValidateCode">测试获取验证码</button>
                <text class="result" :class="validateResult.success ? 'success' : 'error'">
                    {{ validateResult.message }}
                </text>
            </view>

            <view class="test-item">
                <button class="test-btn" @click="testLogin">测试登录API</button>
                <text class="result" :class="loginResult.success ? 'success' : 'error'">
                    {{ loginResult.message }}
                </text>
            </view>

            <view class="test-item">
                <button class="test-btn" @click="testUserInfo">测试用户信息</button>
                <text class="result" :class="userInfoResult.success ? 'success' : 'error'">
                    {{ userInfoResult.message }}
                </text>
            </view>
        </view>

        <view class="test-section">
            <view class="section-title">AI医生API (4545端口)</view>

            <view class="test-item">
                <button class="test-btn" @click="testAiDoctors">测试最近医生</button>
                <text class="result" :class="aiDoctorsResult.success ? 'success' : 'error'">
                    {{ aiDoctorsResult.message }}
                </text>
            </view>

            <view class="test-item">
                <button class="test-btn" @click="testAiChat">测试AI对话</button>
                <text class="result" :class="aiChatResult.success ? 'success' : 'error'">
                    {{ aiChatResult.message }}
                </text>
            </view>
        </view>

        <view class="test-section">
            <view class="section-title">直接测试8080端口</view>

            <view class="test-item">
                <button class="test-btn" @click="testDirect8080">直接测试8080</button>
                <text class="result" :class="direct8080Result.success ? 'success' : 'error'">
                    {{ direct8080Result.message }}
                </text>
            </view>
        </view>

        <view class="test-section">
            <view class="section-title">直接测试4545端口</view>

            <view class="test-item">
                <button class="test-btn" @click="testDirect4545">直接测试4545</button>
                <text class="result" :class="direct4545Result.success ? 'success' : 'error'">
                    {{ direct4545Result.message }}
                </text>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            validateResult: { message: '未测试', success: null },
            loginResult: { message: '未测试', success: null },
            userInfoResult: { message: '未测试', success: null },
            aiDoctorsResult: { message: '未测试', success: null },
            aiChatResult: { message: '未测试', success: null },
            direct8080Result: { message: '未测试', success: null },
            direct4545Result: { message: '未测试', success: null }
        }
    },
    methods: {
        async testValidateCode() {
            this.validateResult = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: '/health-api/med-front-user/userFront/getValidate?userUnique=test123',
                        method: 'GET',
                        success: (response) => {
                            console.log('验证码API响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('验证码API错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.validateResult = {
                        message: `✅ 成功 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.validateResult = {
                        message: `❌ 失败 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.validateResult = {
                    message: `❌ 错误: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testLogin() {
            this.loginResult = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: '/health-api/med-front-user/userFront/login?account=123&password=123456&code=1234&codeKey=test',
                        method: 'GET',
                        success: (response) => {
                            console.log('登录API响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('登录API错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.loginResult = {
                        message: `✅ 成功 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.loginResult = {
                        message: `❌ 失败 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.loginResult = {
                    message: `❌ 错误: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testUserInfo() {
            this.userInfoResult = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: '/health-api/med-front-mine/mine/queryUser?userId=user_001',
                        method: 'GET',
                        success: (response) => {
                            console.log('用户信息API响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('用户信息API错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.userInfoResult = {
                        message: `✅ 成功 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.userInfoResult = {
                        message: `❌ 失败 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.userInfoResult = {
                    message: `❌ 错误: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testAiDoctors() {
            this.aiDoctorsResult = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: '/ai-doctor/recent-doctors?memoryId=test123',
                        method: 'GET',
                        success: (response) => {
                            console.log('AI医生API响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('AI医生API错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.aiDoctorsResult = {
                        message: `✅ 成功 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.aiDoctorsResult = {
                        message: `❌ 失败 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.aiDoctorsResult = {
                    message: `❌ 错误: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testAiChat() {
            this.aiChatResult = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: '/ai-doctor/chat?memoryId=test123&message=你好&userId=1',
                        method: 'POST',
                        success: (response) => {
                            console.log('AI对话API响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('AI对话API错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.aiChatResult = {
                        message: `✅ 成功 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.aiChatResult = {
                        message: `❌ 失败 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.aiChatResult = {
                    message: `❌ 错误: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testDirect8080() {
            this.direct8080Result = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: 'http://127.0.0.1:8080/med-front-user/userFront/getValidate?userUnique=test123',
                        method: 'GET',
                        success: (response) => {
                            console.log('直接8080响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('直接8080错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.direct8080Result = {
                        message: `✅ 8080端口正常 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.direct8080Result = {
                        message: `❌ 8080端口异常 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.direct8080Result = {
                    message: `❌ 8080端口无法访问: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        },

        async testDirect4545() {
            this.direct4545Result = { message: '测试中...', success: null };
            try {
                const res = await new Promise((resolve, reject) => {
                    uni.request({
                        url: 'http://127.0.0.1:4545/ai-doctor/recent-doctors?memoryId=test123',
                        method: 'GET',
                        success: (response) => {
                            console.log('直接4545响应:', response);
                            resolve(response);
                        },
                        fail: (err) => {
                            console.error('直接4545错误:', err);
                            reject(err);
                        }
                    });
                });

                if (res.statusCode === 200) {
                    this.direct4545Result = {
                        message: `✅ 4545端口正常 - 状态码: ${res.statusCode}`,
                        success: true
                    };
                } else {
                    this.direct4545Result = {
                        message: `❌ 4545端口异常 - 状态码: ${res.statusCode}`,
                        success: false
                    };
                }
            } catch (error) {
                this.direct4545Result = {
                    message: `❌ 4545端口无法访问: ${error.errMsg || '网络错误'}`,
                    success: false
                };
            }
        }
    }
}
</script>

<style>
.test-container {
    padding: 20rpx;
    background-color: #f5f5f5;
    min-height: 100vh;
}

.header {
    text-align: center;
    padding: 40rpx 0;
}

.title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
}

.test-section {
    background: white;
    margin: 20rpx 0;
    border-radius: 12rpx;
    padding: 30rpx;
}

.section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #666;
    margin-bottom: 30rpx;
    border-bottom: 2rpx solid #eee;
    padding-bottom: 20rpx;
}

.test-item {
    display: flex;
    align-items: center;
    margin-bottom: 30rpx;
    justify-content: space-between;
}

.test-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 8rpx;
    padding: 20rpx 30rpx;
    font-size: 28rpx;
    min-width: 200rpx;
}

.result {
    font-size: 26rpx;
    flex: 1;
    margin-left: 20rpx;
}

.result.success {
    color: #52c41a;
}

.result.error {
    color: #ff4d4f;
}
</style>
